#include "Enemy.h"

Ghost::Ghost()
{
	GameObject::GameObject();
	right = true;
	left = up = down = false;

	m_velocity.m_x = 0;
	m_velocity.m_y = -1;

	num = 3;

	m_idSprite = 8;
	id_magic_sprite = m_idSprite;

	timeDelay = 10;
	dt = 0.5;
	timeOfGame = 0;
	m_radius = 10;
	m_position.x = 160;
	m_position.y = 460;
}

Ghost::~Ghost()
{
	// do nothing for now
}

void Ghost::setPositionPacman(Point &_positionPacman)
{
	m_Point.x = _positionPacman.x;
	m_Point.y = _positionPacman.y;
}


void Ghost::update()
{
	int ax = 4;
	num = 3;
	int cross = 2;
	

	int ab = m_position.x;
	int ac = m_position.y;

	// direction right
	if (m_velocity.m_x == 1)
	{
		left = true;
		right = down = up = true;
		if (CheckUp(ab, ac) == 1)
		{
			up = false;
			num--;
			cross--;
		}
		if (CheckDown(ab, ac) == 1 && m_velocity.m_x == 1)
		{
			down = false;
			num--;
			cross--;
		}
		if (CheckRight(ab, ac) == 1 && m_velocity.m_x == 1)
		{
			right = false;
			num--;
			cross += 2;
		}
	}

	// direction left
	else if (m_velocity.m_x == -1)
	{
		right = true;
		left = down = up = true;
		if (CheckUp(ab, ac) == 1)
		{
			up = false;
			num--;
			cross--;
		}

		if (CheckDown(ab, ac) == 1 && m_velocity.m_x == -1)
		{
			down = false;
			num--;
			cross--;
		}
		if (CheckLeft(ab, ac) == 1 && m_velocity.m_x == -1)
		{
			left = false;
			num--;
			cross += 2;
		}
	}

	// direction down
	else if (m_velocity.m_y == 1)
	{
		up = true;
		right = left = down = true;
		if (CheckLeft(ab, ac) == 1)
		{
			left = false;
			num--;
			cross--;
		}
		if (CheckRight(ab, ac) == 1 && m_velocity.m_y == 1)
		{
			right = false;
			num--;
			cross--;
		}
		if (CheckDown(ab, ac) == 1 && m_velocity.m_y == 1)
		{
			down = false;
			num--;
			cross += 2;
		}
	}
	else if (m_velocity.m_y == -1)
	{
		down = true;
		right = left = up = true;
		if (CheckLeft(ab, ac) == 1)
		{
			left = false;
			num--;
			cross--;
		}
		if (CheckRight(ab, ac) == 1 && m_velocity.m_y == -1)
		{
			right = false;
			num--;
			cross--;
		}
		if (CheckUp(ab, ac) == 1 && m_velocity.m_y == -1)
		{
			up = false;
			num--; 
			cross += 2;
		}
	}
	
	if (num > 1)
	{
		
		int x = m_position.x - m_Point.x;
		int y = m_position.y - m_Point.y;
		if (x > y)
		{
			
			if (x > 0 && y > 0)
			{
				if (left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
					//moveLeft();
				}
				else if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					// moveUp();
				}
				
			}
			else if (x < 0 && y < 0)
			{
				if (down == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = 1;
					// moveDown();
				}
				else if (right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
					// moveRight();
				}
			}
			else if (x > 0 && y < 0)
			{
				if (left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
					// moveLeft();
				}
				else if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					// moveUp();
				}
				
			}
		}
		// done

		else if (y > x)
		{
			if (x > 0 && y > 0)
			{
				/*m_velocity.m_x = 0;
				m_velocity.m_y = -1;*/
				if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					//moveUp();
				}
				else if (left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
					// moveLeft();
				}
			
			}
			else if (x < 0 && y < 0)
			{
				/*m_velocity.m_x = 1;
				m_velocity.m_y = 0;*/
				if (right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
					// moveRight();
				}
				else if (down == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = 1;
					// moveDown();
				}
				
			}
			else if (x < 0 && y > 0)
			{
				if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					// moveUp();
				}
				else if (right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
					// moveRight();
				}
				
			}
		}
		if (x == 0 || y == 0)
		{
			if (x == 0)
			{
				if (y > 0 && up == true )
				{
					m_velocity.m_y = -1;
					m_velocity.m_x = 0;
				}
				else if (y > 0 && up == false)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
				}
				else if (y < 0 && down == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = 1;
				}
				else if (y < 0 && down == false)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
				}
			}
			else if (y == 0)
			{
				if (x > 0 && left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
				}
				else if (x < 0 && right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
				}
				else if (right == false)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
				}
			} 
		}
	}
	else if (cross == 3)
	{
		if (m_velocity.m_y == -1)
		{
			if (right == true)
			{
				m_velocity.m_x = 1;
				m_velocity.m_y = 0;
			}
			else if (left == true)
			{
				m_velocity.m_x = -1;
				m_velocity.m_y = 0;
			}
		}
		else if (m_velocity.m_x == 1)
		{
			if (up == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = -1;
			}
			else if (down == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = 1;
			}
		}
		else if (m_velocity.m_x == -1)
		{
			if (up == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = -1;
			}
			else if (down == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = 1;
			}
		}
		else if (m_velocity.m_y == 1)
		{
			if (right == true)
			{
				m_velocity.m_x = 1;
				m_velocity.m_y = 0;
			}
			else if (left == true)
			{
				m_velocity.m_x = -1;
				m_velocity.m_y = 0;
			}
		}
	}
	if (m_velocity.m_x == 0 && m_velocity.m_y == 1)
	{
		if (mAnimation == true)
		{
			m_idSprite = id_magic_sprite = 10;
		}
	}
	else if (m_velocity.m_x == 0 && m_velocity.m_y == -1)
	{
		
		if (mAnimation == true)
		{
			m_idSprite = id_magic_sprite = 14;
		}
	}
	else if (m_velocity.m_x == 1 && m_velocity.m_y == 0)
	{
		if (mAnimation == true)
		{
			m_idSprite = id_magic_sprite = 8;
		}
	}
	else if (m_velocity.m_x == -1 && m_velocity.m_y == 0)
	{
		if (mAnimation == true)
		{
			m_idSprite = id_magic_sprite = 12;
		};
	}

	timeOfGame += dt;
	if (timeOfGame > timeDelay)
	{
		m_idSprite++;
		timeOfGame = 0;
		mAnimation = false;
		if (m_idSprite > id_magic_sprite + 1)
		{
			m_idSprite = id_magic_sprite;
			mAnimation = true;
		}
	}
	//////////////////////////////////////////////////////////////


	m_position.x = m_position.x + m_velocity.m_x;
	m_position.y = m_position.y + m_velocity.m_y;

	int acx = m_position.x;
	int acy = m_position.y;

	int w = 20;
	int h = 20;

	m_sprite.setDrawingCoordinates(acx, acy, w, h, m_idSprite);
	box.x = m_position.x;
	box.y = m_position.y;
}